﻿using System;
using System.Net;
using System.Collections.Generic;
using SCG.General;

namespace SCG.SolarVengeanceEngine.Orders
{
    public class PatrolOrder : StarShipOrder
    {
        //constructors        
        public PatrolOrder(SVGame game, OrderType ot)
            : base(game, ot)
        {      
            Direction = 1;
        }
        public PatrolOrder(SVGame game, OrderType ot, SVTokenizer tok)
            : base(game, ot)
        {
            int num = tok.TokenInt;
            NextPoint = tok.TokenInt;
            Direction = tok.TokenInt;
            while (num > 0)
            {
                int x = tok.TokenInt;
                int y = tok.TokenInt;
                _patrolPoints.Add(new SCGPoint(x, y));
                num--;
            }
        }

        //The list of points in the route
        public List<SCGPoint> PatrolPoints
        {
            get
            {
                return _patrolPoints;
            }
        }

        //The next patrol point in the route
        public int NextPoint { get; set; }

        //The current direction the ship is moving in the route
        public int Direction { get; set; }

        //Advance to the next point in the route
        public void Advance()
        {
            if (Direction == 1)
            {
                if (NextPoint == PatrolPoints.Count - 1)
                {
                    if (PatrolPoints[0] == PatrolPoints[PatrolPoints.Count - 1])
                        NextPoint = 1;
                    else
                    {
                        Direction = -1;
                        NextPoint--;
                    }
                }
                else
                    NextPoint++;
            }
            else
            {
                if (NextPoint == 0)
                {
                    Direction = 1;
                    NextPoint++;
                }
                else
                    NextPoint--;
            }
        }

        //persistence  
        public override string Persist()
        {
            SVTokenizer tok = new SVTokenizer(Game);
            tok.AppendToken(_patrolPoints.Count);
            tok.AppendToken(NextPoint);
            tok.AppendToken(Direction);
            foreach (SCGPoint pt in _patrolPoints)
            {
                int x = (int)pt.X;
                int y = (int)pt.Y;
                tok.AppendToken(x);
                tok.AppendToken(y);
            }
            return base.Persist() + tok.Result;
        }

        //description
        public override string ToString()
        {
            int n = PatrolPoints.Count;
            if (PatrolPoints[0] == PatrolPoints[PatrolPoints.Count - 1])
                n--;
            return "Patrol (" + n + " Points)";
        }

        //private members
        private List<SCGPoint> _patrolPoints = new List<SCGPoint>();    
    }
}
